package melon.saber.core.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

public class CollectionUtil {
	
	/**
	 * function 把多个或一个元素封装成一个List
	 * @param args
	 * @return
	 */
	public static <T> List<T> wrapCollection(T...args) {
		List<T> list = new ArrayList<T>();
		for (T t : args) {
			list.add(t);
		}
		return list;
	}
	
	public static <T> List<T> wrapLinkList(T...args) {
		List<T> list = new LinkedList<T>();
		for (T t : args) {
			list.add(t);
		}
		return list;
	}
	
	public static <T> List<T> wrapArray(T[] array) {
		List<T> list = new ArrayList<T>(array.length);
		for (T t : array) {
			list.add(t);
		}
		return list;
	}
	
	/**
	 * function 把List转化成Map
	 * @param list
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> Map<Integer, T> translateToMap(List<? extends T> list, String idKey) {
		Map<Integer, T> map = new HashMap<Integer, T>();
		for (Object obj : list) {
			Class<?> clazz = obj.getClass();
			try {
				Field field = clazz.getDeclaredField(idKey);
				field.setAccessible(true);
				map.put(field.getInt(obj), (T) obj);
				field.setAccessible(false);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return map;
	}
	
	/**
	 * json解析的时候会用到
	 * @param <V>
	 * @param param
	 * @return
	 */
	public static <V> Map<String, V> translateMapKeyString(Map<Integer, V> param) {
		if (isEmpty(param)) return new HashMap<String, V>();
		Map<String, V> newParam = new HashMap<String, V>(param.size());
		Iterator<Integer> iterator = param.keySet().iterator();
		while (iterator.hasNext()) {
			Integer key = iterator.next();
			newParam.put(String.valueOf(key), param.get(key));
		}
		return newParam;
	}
	
	/**
	 * @function 把Map转化成List
	 * @param equips
	 * @return
	 */
	public static <T> List<T> translateToList(Map<Integer, T> equips) {
		if (equips == null || equips.size() == 0) 
			return Collections.emptyList();
		List<T> list = new ArrayList<T>();
		Set<Integer> keySet = equips.keySet();
		for (Integer key : keySet) {
			list.add(equips.get(key));
		}
		return list;
	}
	
	/**
	 * @function 判断集合是否为空
	 * @param collection
	 * @return
	 */
	public static boolean isEmpty(Collection<?> collection) {
		return collection == null || collection.isEmpty();
	}
	
	public static boolean isEmpty(Map<?, ?> map) {
		return map == null || map.size() == 0;
	}
	
	public static String[] randomPickup(String[] sets, int count) {
		if (count >= sets.length) return sets;
		Random r=new Random();  
        String strarray[] = new String[count];  
        int index = 0;
        for(int i = 0; i < count; i++){  
            //刚开始从数组中随机抽取一个  
            //而后将抽取的元素后面的元素向前推进到随机的位置[index位置]  
            //随着循环的继续,逐渐抛弃后面的元素  
            index = r.nextInt(sets.length - i);  
            strarray[i] = sets[index];  
            //元素向前推进到随机[index]的位置  
            for(int j = index; j < sets.length-i-1; j++){  
            	sets[j]=sets[j+1];  
            }  
        }
		return strarray;
	}
	
}
